#!/bin/bash
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/sys_check.sh"
if [ -f /etc/os-release ]; then
   echo "Linux system."
elif [ -f /System/Library/CoreServices/SystemVersion.plist ]; then
   echo "macOS system."
   if [ -f ~/.bash_profile ]; then
      source ~/.bash_profile
    fi
elif current_os_is_windows; then
   echo "Windows system."
else
   echo "Unknown OS"
fi

# APP_INSTALL_NAME="index-tts"
APP_TARGET_DIR_NAME="index-tts"

echo "lmdprogress:3"
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/install_conda.sh"
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/conda_run.sh"
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/conda_and_pip_settings.sh"
# source "$LMD_BASE_INSTALL_SCRIPT_DIR/apps/$APP_INSTALL_NAME/env"

export CURRENT_APP_PATH="${LMD_APPS_DIR}/${APP_TARGET_DIR_NAME}"
echo "$CURRENT_APP_PATH"
mkdir -p "$CURRENT_APP_PATH"

echo "lmdprogress:8"

if [ -d "$CURRENT_APP_PATH" ]; then
    echo "App Dir: $CURRENT_APP_PATH"
else
    echo "Error, App Dir dose not exist, please retry. $CURRENT_APP_PATH"
    exit 1
fi

echo "lmdprogress:12"

export GIT_LFS_SKIP_SMUDGE=1

export GIT_HOST="https://gitee.com"
export GIT_REPO_URL="${GIT_HOST}/lmdown/index-tts-1.5"

export GIT_REPO_BRANCH="master"
export LOCAL_REPO_PATH="$CURRENT_APP_PATH"
sh "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/git_clone_and_fetch.sh"

export GITHUB_GIT_HOST=${GITHUB_PROXY}"https://github.com"
export GITHUB_GIT_REPO_URL="${GITHUB_GIT_HOST}/index-tts/index-tts"

echo "lmdprogress:40"

cd "$CURRENT_APP_PATH"
CONDA_PREFIX="./venv"

if [ -d "$CONDA_PREFIX" ]; then
    echo "found ./venv. remove it. "
    rm -rf "$CONDA_PREFIX"
fi
# INDEX_TTS_ENV_NAME=$APP_INSTALL_NAME
export PYTHON_VER_ENV=3.10
if [ -f /System/Library/CoreServices/SystemVersion.plist ]; then
    conda_create_env $CONDA_PREFIX python=$PYTHON_VER_ENV pip=25.2 -y
elif current_os_is_windows; then
    conda_create_env $CONDA_PREFIX python=$PYTHON_VER_ENV -y
fi

echo "lmdprogress:45"


function install_ffmpeg {
  source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/install_ffmpeg.sh"
}

function check_ffmpeg_and_install_win {
  PROGRAMS=("ffmpeg" "ffprobe")
  missing_programs=0

  for PROGRAM in "${PROGRAMS[@]}"; do
    if ! command -v ${PROGRAM} >/dev/null 2>&1; then
      $PROGRAM -v
      echo "$PROGRAM not found"
      missing_programs=1
    fi
  done

  if [ $missing_programs -eq 1 ]; then
    echo "need install ffmpeg and ffprobe。"
    install_ffmpeg
  else
    echo "ffmpeg and ffprobe exist"
  fi
}

install_torch() {
    if [ -f /System/Library/CoreServices/SystemVersion.plist ]; then
        # conda_run_pip_install --pre torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/nightly/cpu
        source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/install_torch_mac.sh"
        conda_run_conda_forge_install ffmpeg
    elif current_os_is_windows; then
        source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/install_torch_multi_method.sh"
        check_ffmpeg_and_install_win
    fi
}
install_torch

echo "lmdprogress:50"

conda_run_pip_install py-cpuinfo
echo "lmdprogress:51"
if [ -f /System/Library/CoreServices/SystemVersion.plist ]; then
    echo "macOS system."
    conda_run_pip_install wetext
    conda_run_pip_install deepspeed
    # 
elif current_os_is_windows; then
    echo "Windows system."
    conda_run_pip_install "$GITHUB_GIT_HOST/daswer123/deepspeed-windows/releases/download/13.1/deepspeed-0.13.1+cu121-cp310-cp310-win_amd64.whl"
    conda_run_conda_forge_install pynini==2.1.5
    
    echo "lmdprogress:60"
    conda_run_pip_install WeTextProcessing==1.0.3
fi

echo "lmdprogress:66"

# create temp file
rm -rf requirements-temp.txt
cp requirements.txt requirements-temp.txt
grep -v "Darwin" requirements.txt > requirements-temp.txt

conda_run_pip_install -r requirements-temp.txt httpx==0.28.1

echo "lmdprogress:67"
conda_run_pip_install huggingface_hub hf_xet filelock fsspec httpx==0.28.1 modelscope


echo "lmdprogress:70"
conda_run_pip_install -e ".[webui]"

echo "lmdprogress:75"

# download models
if [ -z "$HF_ENDPOINT" ] && [ -n "$HF_MIRROR" ]; then
    export HF_ENDPOINT="$HF_MIRROR"
fi

echo "lmdprogress:80"

# if [ -n "$HF_ENDPOINT" ]; then
#   MODELSCOPE_DISABLE_SYMLINKS=1
#   modelscope download --model black-forest-labs/FLUX.1-dev ae.safetensors --local_dir ./models/vae
# else  
# fi

# model v1.0
# conda run -p $CONDA_PREFIX --no-capture-output huggingface-cli download IndexTeam/Index-TTS \
#   bigvgan_discriminator.pth bigvgan_generator.pth bpe.model dvae.pth gpt.pth unigram_12000.vocab \
#   --local-dir checkpoints

# model v1.5
LOCAL_MODEL_DIR_v1_5="checkpoints-1.5"

if [ -n "$HF_ENDPOINT" ]; then
    conda_run_cmd modelscope download --model IndexTeam/IndexTTS-1.5 --local_dir $LOCAL_MODEL_DIR_v1_5
else
    conda_run_hf_download IndexTeam/IndexTTS-1.5 --local-dir $LOCAL_MODEL_DIR_v1_5
fi

echo "lmdprogress:100"


